//
//  DHButton.m
//  NavigationDemo
//
//  Created by 商广硕 on 2022/7/9.
//

#import "DHButton.h"

@implementation DHButton

- (void)modifyButtonEdgeInsets{
//    [self setBackgroundColor:[UIColor redColor]];
    CGSize  buttonTitleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize  buttonImageSize = [self imageForState:UIControlStateSelected].size;
    switch (self.customButtonType) {
            
        case  CustomButtonTypeLeft:
            self.imageEdgeInsets = UIEdgeInsetsZero;
            self.titleEdgeInsets = UIEdgeInsetsZero;
            break;
            
        case  CustomButtonTypeRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, buttonTitleSize.width+self.customBorder,0, -buttonTitleSize.width-self.customBorder);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -buttonImageSize.width,0, buttonImageSize.width);
            break;
            
        case  CustomButtonTypeUpDown:
            self.imageEdgeInsets=UIEdgeInsetsMake(-buttonTitleSize.height/2,buttonTitleSize.width/2, buttonTitleSize.height/2, -buttonTitleSize.width/2);
            
            self.titleEdgeInsets=UIEdgeInsetsMake(buttonImageSize.height/2+self.customBorder, -buttonImageSize.width/2, -buttonImageSize.height/2-self.customBorder, buttonImageSize.width/2);
            break;
            
        default:
            break;
    }
    
    
}

- (void)refreshView{
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [self modifyButtonEdgeInsets];
}

- (void)setCustomImage:(NSString *)customImage{
    _customImage = [customImage copy];
    [self setImage:[UIImage imageNamed:_customImage] forState:UIControlStateNormal];
    [self refreshView];
}

- (void)setCustomTitle:(NSString *)customTitle{
    _customTitle = [customTitle copy];
    [self setTitle:_customTitle forState:UIControlStateNormal];
    [self refreshView];
}

- (void)setCustomBorder:(NSInteger)customBorder{
    _customBorder = customBorder;
    [self refreshView];
}

- (void)setCustomButtonType:(CustomButtonType)customButtonType{
    _customButtonType = customButtonType;
    [self refreshView];
}
-(void) setCustomBackgroundColor:(UIColor *)customBackgroundColor{
    _customBackgroundColor =customBackgroundColor;
    [self setBackgroundColor:_customBackgroundColor];
     [self refreshView];
}

- (void)setCustomRadius:(NSInteger)customRadius{
    _customRadius =customRadius;
    self.layer.borderWidth=1;
    self.layer.cornerRadius = _customRadius;
    [self refreshView];
}
- (void)setCustomBorderColorr:(UIColor *)customBorderColorr{
    _customBorderColorr =customBorderColorr;
    self.layer.borderColor = _customBorderColorr.CGColor;
//    self.layer.cornerRadius = _customRadius;
    [self refreshView];
}








@end
